package com.ruoyi.cderp.service.impl;

import java.util.List;

import com.ruoyi.cderp.domain.TblSaleOutSheet;
import com.ruoyi.cderp.domain.TblSaleOutSheetDetail;
import com.ruoyi.cderp.mapper.TblSaleOutSheetMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.TblLogisticsSheetDetail;
import com.ruoyi.cderp.mapper.TblLogisticsSheetMapper;
import com.ruoyi.cderp.domain.TblLogisticsSheet;
import com.ruoyi.cderp.service.ITblLogisticsSheetService;

/**
 * 物流单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-25
 */
@Service
public class TblLogisticsSheetServiceImpl implements ITblLogisticsSheetService
{
    @Autowired
    private TblLogisticsSheetMapper tblLogisticsSheetMapper;
    @Autowired
    private TblSaleOutSheetMapper tblSaleOutSheetMapper;

    /**
     * 查询物流单
     *
     * @param id 物流单主键
     * @return 物流单
     */
    @Override
    public TblLogisticsSheet selectTblLogisticsSheetById(String id)
    {
        TblLogisticsSheet tblLogisticsSheet=tblLogisticsSheetMapper.selectTblLogisticsSheetById(id);
        for (TblLogisticsSheetDetail tblLogisticsSheetDetail:tblLogisticsSheet.getTblLogisticsSheetDetailList()){
            tblLogisticsSheetDetail.setTblSaleOutSheet(tblSaleOutSheetMapper.selectTblSaleOutSheetById(tblLogisticsSheetDetail.getBizId()));
        }

        return tblLogisticsSheet;
    }

    /**
     * 查询物流单列表
     *
     * @param tblLogisticsSheet 物流单
     * @return 物流单
     */
    @Override
    public List<TblLogisticsSheet> selectTblLogisticsSheetList(TblLogisticsSheet tblLogisticsSheet)
    {
        List<TblLogisticsSheet> list = tblLogisticsSheetMapper.selectTblLogisticsSheetList(tblLogisticsSheet);
        for (TblLogisticsSheet tblLogisticsSheet1:list){
            for (TblLogisticsSheetDetail tblLogisticsSheetDetail:tblLogisticsSheet1.getTblLogisticsSheetDetailList()){
                tblLogisticsSheetDetail.setTblSaleOutSheet(tblSaleOutSheetMapper.selectTblSaleOutSheetById(tblLogisticsSheetDetail.getBizId()));
            }
        }
        return list;
    }

    /**
     * 新增物流单
     *
     * @param tblLogisticsSheet 物流单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTblLogisticsSheet(TblLogisticsSheet tblLogisticsSheet)
    {
        tblLogisticsSheet.setCreateTime(DateUtils.getNowDate());
        int rows = tblLogisticsSheetMapper.insertTblLogisticsSheet(tblLogisticsSheet);
        insertTblLogisticsSheetDetail(tblLogisticsSheet);
        return rows;
    }

    /**
     * 修改物流单
     *
     * @param tblLogisticsSheet 物流单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTblLogisticsSheet(TblLogisticsSheet tblLogisticsSheet)
    {


        tblLogisticsSheet.setUpdateTime(DateUtils.getNowDate());
        tblLogisticsSheetMapper.deleteTblLogisticsSheetDetailBySheetId(tblLogisticsSheet.getId());
        insertTblLogisticsSheetDetail(tblLogisticsSheet);
        return tblLogisticsSheetMapper.updateTblLogisticsSheet(tblLogisticsSheet);
    }

    /**
     * 批量删除物流单
     *
     * @param ids 需要删除的物流单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblLogisticsSheetByIds(String[] ids)
    {
        tblLogisticsSheetMapper.deleteTblLogisticsSheetDetailBySheetIds(ids);
        return tblLogisticsSheetMapper.deleteTblLogisticsSheetByIds(ids);
    }

    /**
     * 删除物流单信息
     *
     * @param id 物流单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblLogisticsSheetById(String id)
    {
        tblLogisticsSheetMapper.deleteTblLogisticsSheetDetailBySheetId(id);
        return tblLogisticsSheetMapper.deleteTblLogisticsSheetById(id);
    }

    @Override
    public int isDelivery(String id) {
        return tblLogisticsSheetMapper.isDelivery(id);
    }

    /**
     * 新增物流单明细信息
     *
     * @param tblLogisticsSheet 物流单对象
     */
    public void insertTblLogisticsSheetDetail(TblLogisticsSheet tblLogisticsSheet)
    {
        List<TblLogisticsSheetDetail> tblLogisticsSheetDetailList = tblLogisticsSheet.getTblLogisticsSheetDetailList();
        String id = tblLogisticsSheet.getId();
        if (StringUtils.isNotNull(tblLogisticsSheetDetailList))
        {
            List<TblLogisticsSheetDetail> list = new ArrayList<TblLogisticsSheetDetail>();
            for (TblLogisticsSheetDetail tblLogisticsSheetDetail : tblLogisticsSheetDetailList)
            {
                tblLogisticsSheetDetail.setSheetId(id);
                list.add(tblLogisticsSheetDetail);
            }
            if (list.size() > 0)
            {
                tblLogisticsSheetMapper.batchTblLogisticsSheetDetail(list);
            }
        }
    }
}
